package com.wsoft.portal.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.portal.entity.PortalModuleEntity;
import com.wsoft.portal.mapper.PortalModuleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/12/27 10:09
 */
@Service
public class PortalModuleManager extends ServiceImpl<PortalModuleMapper, PortalModuleEntity> {

    @Resource
    private RedisService redisService;

    @Override
    public boolean updateById(PortalModuleEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Portal.MODULE_ID + entity.getId());
            redisService.del(RedisKeyConstant.Portal.MODULE_LIST + entity.getPortalId());
        }
        return b;
    }

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    public boolean save(PortalModuleEntity entity){
        boolean b = super.save(entity);
        if(b){
            redisService.del(RedisKeyConstant.Portal.MODULE_LIST + entity.getPortalId());
        }
        return b;
    }


    /**
     * 批量保存
     * @param list
     * @return
     */
    public boolean saveBatch(List<PortalModuleEntity> list){
        boolean b = super.saveBatch(list);
        if(b){
            for (PortalModuleEntity entity : list) {
                redisService.del(RedisKeyConstant.Portal.MODULE_LIST + entity.getPortalId());
            }
        }
        return b;
    }

    /**
     * 批量修改
     * @param entityList
     * @return
     */
    public boolean updateBatchById(List<PortalModuleEntity> entityList){
        boolean b = super.updateBatchById(entityList);
        if(b){
            redisService.deleteByKeyPattern(RedisKeyConstant.Portal.MODULE + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }

    /**
     * 根据id 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        PortalModuleEntity entity = getById(id);
        boolean b =  super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.Portal.MODULE_ID + id);
            redisService.del(RedisKeyConstant.Portal.MODULE_LIST + entity.getPortalId());
        }
        return b;
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Portal.MODULE_ID,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public PortalModuleEntity getById(Long id){
        return super.getById(id);
    }

    @GetRedisCache(type = RedisKeyConstant.Portal.MODULE_LIST,
            expire_sec = RedisKeyConstant.Portal.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<PortalModuleEntity> getByPortalId(Long portalId){
        QueryWrapper<PortalModuleEntity> query = new QueryWrapper<>();
        query.lambda().eq(PortalModuleEntity::getPortalId,portalId)
                .orderByAsc(PortalModuleEntity::getSort);
        return list(query);
    }
}
